package com.bangcle.library.utils;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AlignmentSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

public class TextSpanBuilder extends SpannableStringBuilder {

    public TextSpanBuilder() {
        super();
    }
    public TextSpanBuilder append(String text, int mTextStyle, float mRelativeTextSize, int mTextColor, int mBgColor) {
        return this.append(text, mTextStyle, mRelativeTextSize, mTextColor, mBgColor, null);
    }
    public TextSpanBuilder append(String text, int mTextStyle, float mRelativeTextSize, int mTextColor, int mBgColor,Layout.Alignment align) {
        SpannableString styleString = new SpannableString(text);
        if (mTextStyle != 0)
            styleString.setSpan(new StyleSpan(mTextStyle), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (mRelativeTextSize  != 0)
            styleString.setSpan(new RelativeSizeSpan(mRelativeTextSize), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (mTextColor  != 0)
            styleString.setSpan(new ForegroundColorSpan(mTextColor), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (mBgColor  != 0)
            styleString.setSpan(new BackgroundColorSpan(mBgColor), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (align  != null)
            styleString.setSpan(new AlignmentSpan.Standard(align), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        
        super.append(styleString);
        return this;
    }
    public TextSpanBuilder img(Context mContext,int resid) {
        if (resid<0){
            return this;
        }
        SpannableString styleString = new SpannableString("M");
        styleString.setSpan(new ImageSpan(mContext,resid,DynamicDrawableSpan.ALIGN_BOTTOM){
            private int mDrawableHeight=0;
            @Override
            public Drawable getDrawable() {
                Drawable d=super.getDrawable();
                mDrawableHeight=d.getBounds().height();
                return d;
            }
            @Override
            public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {                
                super.draw(canvas, text, start, end, x, top, y, (top+bottom)/2+mDrawableHeight/2, paint);
            }
        },0,styleString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        super.append(styleString);
        return this;
    }
    @Override
    public TextSpanBuilder append(CharSequence text) {
        super.append(text);
        return this;
    }

    @Override
    public TextSpanBuilder append(CharSequence text, int start, int end) {
        super.append(text);
        return this;
    }

    @Override
    public TextSpanBuilder append(char text) {
        super.append(text);
        return this;
    }

}